package edu.unipd.p2t.core;

import java.util.GregorianCalendar;

import edu.unipd.p2t.R;
import edu.unipd.p2t.connection.ConnectionManager;
import edu.unipd.p2t.connection.Device;
import edu.unipd.p2t.db.DataManager;
import edu.unipd.p2t.db.Message;
import edu.unipd.p2t.db.SQLiteDatabaseAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;

public class MessageGui extends Activity {
	
	private EditText mEditText;
    private Button mSendButton;
    private Activity me;
    private DataManager myDB;
    private static final String PREFS_NAME = "Peer2TweetPreferences";

    
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        me=this;
        //requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.message);
        //getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main);
        myDB=DataManager.getInstance(me);
        //ottengo componenti GUI
        mSendButton = (Button) findViewById(R.id.send);
    	mEditText = (EditText) findViewById(R.id.entry);
    	
    	
    	//imposto oggetti per la connessione Bluetooth
    	//cm=ConnectionManager.getInstance();
    	//cm.seek();
    	//suppongo che ci sia un device e che la connessione vada a buon fine
    	//d=cm.getNextDevice();
    	//d.connect();

        mSendButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	Log.i("MG","Hai scritto:"+mEditText.getText().toString());


            	//ottengo lo UserName
            	SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                String user = settings.getString("UserName","User");
                
            	Message m=new Message(user,mEditText.getText().toString(), new GregorianCalendar().getTime(), 10);
            	myDB.insMessage(m); //hop

            	mEditText.setText("");
            	//Torno alla MainActivity
            	startActivity(new Intent(me,MainActivity.class));
            }
        });

	}
}